using System;
using System.Web.UI.WebControls;
using mojoPortal.Web.UI;
using mojoPortal.Web;

namespace ud_Controls
{
    public partial class ControlsModule : SiteModuleControl
    {
        private ControlsModuleConfiguration config = new ControlsModuleConfiguration();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Load += Page_Load;
            EnableViewState = false;
        }

        protected virtual void Page_Load(object sender, EventArgs e)
        {
            LoadSettings();
            PopulateLabels();
            PopulateControls();
        }

        private void PopulateControls()
        {
            if (config.ShowLoginLinkSetting)
            {
                LoginLink loginLink = new LoginLink();
                placeHolder.Controls.Add(loginLink);
            }
            if (config.ShowLoginNoLinkSetting)
            {
                LoginNoLink loginNoLink = new LoginNoLink();
                placeHolder.Controls.Add(loginNoLink);
            }
            if (config.ShowWelcomeMessageSetting)
            {
                WelcomeMessage welcomeMessage = new WelcomeMessage();
                placeHolder.Controls.Add(welcomeMessage);
            }
            if (config.ShowProfileLinkSetting)
            {
                UserProfileLink profileLink = new UserProfileLink();
                placeHolder.Controls.Add(profileLink);
            }
            if (config.ShowMemberListLinkSetting)
            {
                MemberListLink memberListLink = new MemberListLink();
                placeHolder.Controls.Add(memberListLink);
            }
            if (config.ShowLogoutLinkSetting)
            {
                LogoutLink logoutLink = new LogoutLink();
                placeHolder.Controls.Add(logoutLink);
            }
            if (config.ShowRegisterLinkSetting)
            {
                RegisterLink registerLink = new RegisterLink();
                placeHolder.Controls.Add(registerLink);
            }
            if (config.ShowDateTimeControlSetting)
            {
                Label lblDateTime = new Label();
                double TimeOffset = SiteUtils.GetUserTimeOffset();
                TimeZoneInfo timeZone = SiteUtils.GetUserTimeZone();
                lblDateTime.Text = timeZone != null ? TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, timeZone).ToString(config.DateTimeFormatSetting) : DateTime.UtcNow.AddHours(TimeOffset).ToString(config.DateTimeFormatSetting);
                lblDateTime.CssClass = "datetimelabel";
                placeHolder.Controls.Add(lblDateTime);
            }
            if (config.ShowOnlineStatisticsSetting)
            {
                OnlineStatisticsControl osc =
                    (OnlineStatisticsControl)LoadControl("~/Controls/OnlineStatisticsControl.ascx");
                placeHolder.Controls.Add(osc);
            }
            if (config.ShowOnlineMemberListSetting)
            {
                OnlineMemberListControl omlc =
                    (OnlineMemberListControl) LoadControl("~/Controls/OnlineMemberListControl.ascx");
                placeHolder.Controls.Add(omlc);
            }
            if (config.ShowMembershipStatisticsSetting)
            {
                MembershipStatisticsControl msc =
                    (MembershipStatisticsControl) LoadControl("~/Controls/MembershipStatisticsControl.ascx");
                placeHolder.Controls.Add(msc);
            }
            if (config.ShowCounterSetting)
            {
                CounterControls osc =
                    (CounterControls)LoadControl("~/Counter/CounterControl.ascx");
                osc.ImageSiteRoot = ImageSiteRoot;
                osc.Config = config;
                placeHolder.Controls.Add(osc);
            }
        }

        protected virtual void PopulateLabels()
        {

        }


        protected virtual void LoadSettings()
        {
            pnlContainer.ModuleId = ModuleId;
            config = new ControlsModuleConfiguration(Settings);
            if (!config.CustomCssClass.Equals(string.Empty)) { pnlContainer.CssClass += " " + config.CustomCssClass; }
        }
    }
}
